package com.hongxia.assetsys.controller;

import com.hongxia.assetsys.common.BaseResponse;
import com.hongxia.assetsys.common.ErrorCode;
import com.hongxia.assetsys.common.ResultUtil;
import com.hongxia.assetsys.domain.AssertOrg;
import com.hongxia.assetsys.domain.Asset;
import com.hongxia.assetsys.domain.AssetCategory;
import com.hongxia.assetsys.domain.AssetLabel;
import com.hongxia.assetsys.exception.BusinessException;
import com.hongxia.assetsys.service.AssertOrgService;
import com.hongxia.assetsys.service.AssetCategoryService;
import com.hongxia.assetsys.service.AssetLabelService;
import com.hongxia.assetsys.service.AssetService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController()
@RequestMapping("/asset")
@RequiresRoles(value = {"asset manager", "admin"}, logical = Logical.OR)
@Slf4j
public class AssetController {
    @Autowired
    AssetService assetService;
    @Autowired
    AssetCategoryService categoryService;
    @Autowired
    AssetLabelService labelService;

    @PostMapping
    public BaseResponse<Integer> addAsset(@RequestBody Asset asset) {
        if (asset == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Integer id = assetService.addAsset(asset);
        return ResultUtil.success(id);
    }

    @PutMapping
    public BaseResponse<Integer> updateAsset(@RequestBody Asset asset){
        if(asset == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Integer id = asset.getId();
        assetService.updateById(asset);
        return ResultUtil.success(id);
    }

    @GetMapping("/{id}")
    public BaseResponse<Asset> getAssetById(@PathVariable Integer id){
        if(id < 0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Asset asset = assetService.getById(id);
        return ResultUtil.success(asset);
    }

    @GetMapping("/list")
    public BaseResponse<List<Asset>> getAssetList(){
        List<Asset> assetList = assetService.list();
        return ResultUtil.success(assetList);
    }

    @GetMapping("/search/name")
    public BaseResponse<List<Asset>> getAssetByName(String assetname){
        List<Asset> assetList = assetService.getAssetByName(assetname);
        return ResultUtil.success(assetList);
    }

    @GetMapping("/search/label")
    public BaseResponse<List<Asset>> getAssetByLabel(String assetlabel){
        List<Asset> assetList = assetService.getAssetByLabel(assetlabel);
        return ResultUtil.success(assetList);
    }

    @GetMapping("/search/cate")
    public BaseResponse<List<Asset>> getAssetByCate(String assetcategory){
        List<Asset> assetList = assetService.getAssetByCate(assetcategory);
        return ResultUtil.success(assetList);
    }

    @GetMapping("/search/belongorg")
    public BaseResponse<List<Asset>> getAssetByBelongOrg(String belongorg){
        List<Asset> assetList = assetService.getAssetByBelongOrg(belongorg);
        return ResultUtil.success(assetList);
    }

    @GetMapping("/category")
    public BaseResponse<List<AssetCategory>> getAssetCategoryList(){
        List<AssetCategory> list = categoryService.list();
        return ResultUtil.success(list);
    }

    @PostMapping("/category")
    public BaseResponse<Integer> addAssetCategory(@RequestBody AssetCategory category){
        if(category == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Integer id = assetService.addAssetCategory(category);
        return ResultUtil.success(id);
    }

    @PutMapping("/category")
    public BaseResponse<Integer> updateAssetCategory(@RequestBody AssetCategory category){
        if(category == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Integer id = category.getId();
        categoryService.updateById(category);
        return ResultUtil.success(id);
    }

    @GetMapping("/label")
    public BaseResponse<List<AssetLabel>> getAssetLabelList(){
        List<AssetLabel> list = labelService.list();
        return ResultUtil.success(list);
    }

    @PostMapping("/label")
    public BaseResponse<Integer> addAssetLabel(@RequestBody AssetLabel label){
        if(label == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Integer id = assetService.addAssetLabel(label);
        return ResultUtil.success(id);
    }

    @PutMapping("/label")
    public BaseResponse<Integer> updateAssetLabel(@RequestBody AssetLabel label){
        if(label == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Integer id = label.getId();
        labelService.updateById(label);
        return ResultUtil.success(id);
    }

//    @GetMapping("/search/fatherorg")
//    public BaseResponse<List<AssertOrg>> getOrgListByFatherOrg(String orgname){
//        List<AssertOrg> orgList = orgService.getOrgListByFatherOrg(orgname);
//        return ResultUtil.success(orgList);
//    }
}
